package com.xiexie.seckill.rabbitMQ;

import cn.hutool.json.JSONUtil;
import com.xiexie.seckill.pojo.SeckillMessage;
import com.xiexie.seckill.pojo.SeckillOrder;
import com.xiexie.seckill.pojo.User;
import com.xiexie.seckill.service.IGoodsService;
import com.xiexie.seckill.service.IOrderService;
import com.xiexie.seckill.vo.GoodsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * rabbitMQ 消费消息
 */
@Service
@Slf4j
public class RabbitMQReceiver {

    @RabbitListener(queues = "queue")
    public void receive(Object msg) {
        log.info("接收到消息：" + msg);
    }


    @RabbitListener(queues = "queue_fanout01")
    public void receive01(Object msg) {
        log.info("fanout01接收到消息：" + msg);
    }

    /**
     * fanout模式接收
     * @param msg
     */
    @RabbitListener(queues = "queue_fanout02")
    public void receive02(Object msg) {
        log.info("fanout02接收到消息：" + msg);
    }

    /**
     * direct模式接收
     * @param msg
     */
    @RabbitListener(queues = "queue_direct01")
    public void receive03(Object msg) {
        log.info("queue_direct01接收到消息：" + msg);
    }

    /**
     * topic模式接收
     * @param msg
     */
    @RabbitListener(queues = "queue_topic01")
    public void receive04(Object msg) {
        log.info("queue_topic01接收到消息：" + msg);
    }

    @RabbitListener(queues = "queue_topic02")
    public void receive05(Object msg) {
        log.info("queue_topic02接收到消息：" + msg);
    }


    /**
     * headers模式接收
     * @param msg
     */
    @RabbitListener(queues = "queue_headers01")
    public void receive07(Message msg) {
        log.info("queue_headers01接收到消息：" + new String(msg.getBody()));
    }

    @RabbitListener(queues = "queue_headers02")
    public void receive08(Message msg) {
        log.info("queue_headers02接收到消息：" + new String(msg.getBody()));
    }



    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IOrderService orderService;

    /**
     * 接收业务数据  秒杀信息
     * 进行下单操作
     * @param message
     */
    @RabbitListener(queues = "queue_seckill")
    public void receiveSeckillMessage(String message) {
        log.info("queue_seckill接收到消息：" + message);
        SeckillMessage seckillMessage = JSONUtil.toBean(message, SeckillMessage.class);
        User user = seckillMessage.getUser();
        Long goodsId = seckillMessage.getGoodId();
        GoodsVo goodsVo = goodsService.getGoodsVoByGoodsId(goodsId);
        if (goodsVo.getStockCount() < 1) {
            return;
        }

        // 判断是否重复抢购
        String key = "order:" + user.getId() + ":" + goodsId;
        SeckillOrder seckillOrder = (SeckillOrder)redisTemplate.opsForValue().get(key);
        if (null != seckillOrder) {
            return;
        }

        // 下单操作
        orderService.seckill(user, goodsVo);
    }
}
